package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;

import com.tanhua.common.pojo.UserFreeze;
import com.tanhua.dubbo.server.pojo.BaseUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@Service
public class MangerApiImpl implements ManagerApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    //通过id查看用户详细
    @Override
    public BaseUser findBaseUserById(Long userId) {
        Query query=Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.findOne(query,BaseUser.class);
    }

    //修改状态
    @Override
    public void updateUserStatus(String status, Integer userId) {
        Query query=Query.query(Criteria.where("userId").is(userId));
        Update update=new Update();
        update.set("userStatus",status);
        mongoTemplate.updateFirst(query,update,BaseUser.class);
    }

    //修改是否冻结过
    @Override
    public void updateisFreeze(String status, Integer userId) {
        Query query=Query.query(Criteria.where("userId").is(userId));
        Update update=new Update();
        update.set("isFreeze",status);
        mongoTemplate.upsert(query,update,BaseUser.class);
    }

    //添加解冻原因
    @Override
    public void saveFreezeReason(Long userId,String reasonsForThawing) {
        Query query=Query.query(Criteria.where("userId").is(userId));
        Update update=new Update().set("reasonFreeze",reasonsForThawing);
        mongoTemplate.updateFirst(query,update,BaseUser.class);
    }

    //修改最近登录地点
    @Override
    public void updateBaseUserLastActiveTime(Long userId, String address) {
        Query query=Query.query(Criteria.where("userId").is(userId));
        Update update=new Update();
        update.set("lastActiveTime",address);
        mongoTemplate.updateFirst(query,update,BaseUser.class);
    }
}
